package cn.circle.mobile.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.circle.mobile.service.IUserService;
import cn.circle.system.util.CookieUtils;



public class MobileLoginInterceptor implements HandlerInterceptor{

	@Autowired
	private IUserService userService;
	
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
		// TODO Auto-generated method stub
		//从cookie中取token
		String token = CookieUtils.getCookieValue(arg0, "USER_TOKEN");		
		String phone = userService.getUserByToken(token);
		if(phone == null){			
			arg1.sendRedirect("/mobile/goLogin");
			return false;
		}
		return true;
	}

}
